﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AiFrame.InterfaceLib.MVP;
using AiFrameDemo.Contacts.Core.DataStore;
using AiFrameDemo.Contacts.Core.Views;
using AiFrameDemo.Lib.Model;

namespace AiFrameDemo.Contacts.Core.ViewModel
{
    public class ContactDataViewModel: ViewModelBase
    {
        private IContactDataView _view;
        private IList _customerList;
        private Contact _contact = new Contact() { DateTime = DateTime.Now};

        public ContactDataViewModel(IContactDataView view)
        {
            _view = view;
            _customerList = LoadCustomerList();

            _view.BindToViewModel(this);
        }

        #region Properties

        public IList CustomerListValues
        {
            get
            {
                if (_customerList.Count == 0)
                    _customerList.Add(new KeyValuePair<Customer, string>(null, "<No dataset available>"));

                return _customerList;
            }
            set
            {
                _customerList = value;
                OnPropertyChanged("CustomerListValues");
            }
        }

        public string Content
        {
            get { return _contact.Content; }
            set
            {
                if (_contact.Content == value)
                    return;

                _contact.Content = value;
                OnPropertyChanged("Content");
            }
        }

        public Customer Customer
        {
            get { return _contact.Customer; }
            set
            {
                if (_contact.Customer == value)
                    return;

                _contact.Customer = value;
                OnPropertyChanged("Customer");
            }
        }

        public bool CanSave
        {
            get { return _contact.Customer != null; }
        }

        #endregion

        private IList LoadCustomerList()
        {
            var customerList = DaoFactory.Instance.CustomerStore.FindAll().ToList();
            var list = new ArrayList(customerList.Count);

            foreach (var customer in customerList)
            {
                list.Add(new KeyValuePair<Customer, string>(customer,
                                                            string.Format("{0}: {1} {2}", customer.CustomerNumber,
                                                                          customer.Firstname, customer.Lastname)));
            }
            return list;
        }

        public void UserWantsToSave()
        {
            try
            {
                DaoFactory.Instance.ContactStore.SaveOrUpdate(_contact);
                _view.InformUser("Your data has been successfully saved.");
                _view.Close();
            }
            catch (Exception e)
            {
                _view.DisplayError("Error: " + e.Message);
            }
        }
    }
}
